package com.doit.day03


object _11_聚合类的算子 {
  def main(args: Array[String]): Unit = {
    val list: List[Int] = List(1, 2, 3, 4, 5)

    //    val iterator: Iterator[List[Int]] = list.grouped(3)
    /*    while (iterator.hasNext){
          println(iterator.next())
        }*/
    val orders: List[ScalaOrder] = List(
      ScalaOrder("o001", "sn001", "g001", "c001", 2, 9.9),
      ScalaOrder("o002", "sn002", "g001", "c001", 1, 1400),
      ScalaOrder("o003", "sn003", "g001", "c001", 3, 189),
      ScalaOrder("o005", "sn005", "g002", "c001", 1, 2008),
      ScalaOrder("o004", "sn004", "g002", "c002", 54, 800),
      ScalaOrder("o008", "sn008", "g003", "c002", 3, 356),
      ScalaOrder("o007", "sn007", "g003", "c002", 8, 19.9),
      ScalaOrder("o006", "sn006", "g003", "c002", 1, 39.9)
    )

    //针对品类进行分组 取price * goods_number 的和
    //针对goods_id 进行分组，求price * goods_num 的最大值
    //针对goods_id和category_id 进行分组，求price * goods_num 的最大值

   val fun =  (order:ScalaOrder)=>{(order.goods_id,order.category_id)}

    val map: Map[(String, String), List[ScalaOrder]] = orders.groupBy(fun)


/*    val res:Map[(String,String),Double] = map.map(tp=>{
      val value: (String, String) = tp._1
      val list2: List[ScalaOrder] = tp._2
      val listPrice: List[Double] = list2.map(order => order.price * order.goods_number)
      val max: Double = listPrice.max
      (value,max)
    })*/





/*    val map: Map[String, List[ScalaOrder]] = orders.groupBy(_.goods_id)
    //("g001",List(三个对象))   ("g002",List(俩对象))  ("g003",List(仨对象))
    map.map(tp=>{
      val list3: List[ScalaOrder] = tp._2
      val listDouble: List[Double] = list3.map(scalaOrder => scalaOrder.price * scalaOrder.goods_number)
      (tp._1,listDouble.max)
    }).foreach(println)*/



/*    val map: Map[String, List[ScalaOrder]] = orders.groupBy(_.category_id)
    val res: Map[String, Double] = map.map(tp => {
      //c001
      val cat_id: String = tp._1
      val list1: List[ScalaOrder] = tp._2
      val list2: List[Double] = list1.map(order => order.price * order.goods_number)
      val sum: Double = list2.sum
      (cat_id, sum)
    })*/

/*    val map1: Map[String, Int] = Map(("zss", 19), ("lss", 28), ("zss", 19), ("lss", 20))
    map1.map((a:(String,Int))=>{
      a._1
      a._2
    })*/


  }

}
